<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件
/**
 * @权限验证
 */
function get_auth($rule,$uid){
    $auth=new \auth\Auth();
    if(in_array($uid,config('administrator'))){
        return true;
    }else{
        if($auth->check($rule,$uid)){
            return true;
        }else{
            return false;
        }
    }
}
//生成随机字符串指定长度指定类型
function make_code($type = "num", $length = 6)
{
    // 密码字符集，可任意添加你需要的字符
    if ($type == 'num') {
        $chars = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
    } else {
        $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
            'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
            't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
            'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
            'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
    }
    // 在 $chars 中随机取 $length 个数组元素键名
    $keys = array_rand($chars, $length);
    $password = '';
    for ($i = 0; $i < $length; $i++) {
        // 将 $length 个数组元素连接成字符串
        $password .= $chars[$keys[$i]];
    }
    return $password;
}

//获取一天之内的时间线
function getTimeLine($start = "00:00 AM", $end = "23:30 PM", $step = 60)
{
    $baseTime = strtotime(date("Y-m-d"));
    $step = $step * 60;
    for ($i = $baseTime; $i < $baseTime + 24 * 3600; $i += $step) {
        $baseArray[] = date("H:i A", $i);
    }
    $returnArr = array();
    foreach ($baseArray as $k => $v) {
        if ($v >= $start && $v <= $end) {
            $returnArr[] = $v;
        }
    }
    return $returnArr;

}

//id转化
function idtrans($id, $table, $fild)
{
    if(!$id){
        return "";
    }
    if (is_int($id)) {
        $res = db($table)->where('id', $id)->find();
    } else {
        $res = db($table)->where($id)->find();
    }
    if ($fild) {
        return $res[$fild];
    } else {
        return $res;
    }
}

/*
 * 获取当前连入客户端的真ip地址
 * */
function get_real_ip()
{

    $ip = false;

    if (!empty($_SERVER["HTTP_CLIENT_IP"])) {

        $ip = $_SERVER["HTTP_CLIENT_IP"];

    }

    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {

        $ips = explode(", ", $_SERVER['HTTP_X_FORWARDED_FOR']);

        if ($ip) {

            array_unshift($ips, $ip);
            $ip = FALSE;

        }

        for ($i = 0; $i < count($ips); $i++) {

            if (!eregi("^(10|172\.16|192\.168)\.", $ips[$i])) {

                $ip = $ips[$i];

                break;

            }

        }

    }

    return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);

}

/*
 * 验证当前访问ip是类型并返回
 * 首先验证是内部局域网或127.0.0.1 是内网返回false；
 * 然后验证是国内ip还是国外ip国内true国外false；
 * */
function checkIp()
{
    $ip = get_real_ip();
    $ip = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
    if (!$ip) {
        //内网
        return 1;
    }
    $longIp = floatval(ip2long($ip));
    $list = db('ipdat')->where('start<=' . $longIp . ' and end >=' . $longIp)->find();
    if ($list) {
        //外网国内网
        return 2;
    } else {
        //外网国外网
        return 3;
    }
}
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=false){
    if(function_exists("mb_substr")){
        if($suffix)

            return mb_substr($str, $start, $length, $charset)."...";

        else

            return mb_substr($str, $start, $length, $charset);

    }elseif(function_exists('iconv_substr')) {

        if($suffix)

            return iconv_substr($str,$start,$length,$charset)."...";

        else

            return iconv_substr($str,$start,$length,$charset);

    }

    $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";

    $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";

    $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";

    $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";

    preg_match_all($re[$charset], $str, $match);

    $slice = join("",array_slice($match[0], $start, $length));

    if($suffix) return $slice."…";

    return $slice;

}

